﻿using server.DAO;
using server.Model;
using server.Tool;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace server.Business
{
    /// <summary>
    /// 聊天信息处理
    /// </summary>
    public class ChatHandle
    {
        public string SendChatMessage(string json, RoomDao roomDao)
        {
            string[] strs = json.Split('|');
            string roomId = strs[0];
            string content = strs[1];
            string userName = strs[2];
            string userType = strs[3];
            string userID = strs[4];

            ConcurrentDictionary<PubgSession, SessionItem> dic = PubgSession.mOnLineConnections;
            if (dic == null || dic.Count == 0)
            {
                return "failture";
            }

            //玩家的信息
            if (userType.Equals("0"))
            {
                //获取该房间下的所有用户
                List<Room_User> roomUsers = roomDao.SearchSingleGrounpCommon(roomId, int.Parse(userID));

                //获取管理员
                int AdminUser = roomDao.GetGrounpAdminByRoom(int.Parse(roomId));
                //推送给所有队员
                roomUsers.ForEach((item) =>
                {
                    foreach (PubgSession session in dic.Keys)
                    {
                        SessionItem sessionItem = null;
                        dic.TryGetValue(session, out sessionItem);
                        if (sessionItem != null && sessionItem.gpsItem != null && sessionItem.gpsItem.userId == item.user_id)
                        {

                            StartSendChatMessage(content, userName, session);
                        }

                    }

                });
                //推送给该队的管理员
                foreach (PubgSession session in dic.Keys)
                {
                    SessionItem sessionItem = null;
                    dic.TryGetValue(session, out sessionItem);
                    if (sessionItem != null && sessionItem.gpsItem != null && sessionItem.gpsItem.userId == AdminUser)
                    {

                        StartSendChatMessage(content, userName, session);
                    }

                }
            }
            //管理员
            else
            {
                List<int> userIdList = roomDao.GetRoomUserListByAdmin(int.Parse(userID));

                userIdList.ForEach((userId) =>
                {
                    foreach (PubgSession session in dic.Keys)
                    {
                        SessionItem sessionItem = null;
                        dic.TryGetValue(session, out sessionItem);
                        if (sessionItem != null && sessionItem.gpsItem != null && (
                        sessionItem.gpsItem.userId == userId))
                        {
                            StartSendChatMessage(content, "系统管理员", session);
                        }

                    }

                });
            }
            return "success";
        }


        public void StartSendChatMessage(string content, string userName, PubgSession session)
        {
            Dictionary<string, string> _dic = new Dictionary<string, string>();
            _dic.Add("time", TimeUtils.GetCurrentFormatTime());
            _dic.Add("content", content);
            _dic.Add("name", userName);
            string resultJson = Utils.CollectionsConvert.ToJSON(_dic);
            string data = "SendMessage" + Constant.START_SPLIT + resultJson + "\r\n";
            session.Send(data);
        }
    }
}
